﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary.App_Code;

/// <summary>
/// Summary description for CategoryDAL
/// </summary>
public class CategoryDAL
{
    public static ShopMobileDataContext data = new ShopMobileDataContext();
	public CategoryDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    
    public static List<CategoryBLL> getCategory()
    {
        var ds = from cg in data.Categories
                 select new CategoryBLL(cg.id, cg.name_cat);
        return ds.ToList<CategoryBLL>();
    }
    public static List<CategoryBLL> getCategory2()
    {
        var ds = from cg in data.Categories
                 select new CategoryBLL(cg.name_cat);
        return ds.ToList<CategoryBLL>();
    }
    public static List<CategoryBLL> getCategoryInfo(int category_id)
    {
        var info = from ct in data.Categories
                   where ct.id == category_id
                   select new CategoryBLL(ct.id, ct.name_cat);
        return info.ToList<CategoryBLL>();
    }
    public static int getID(string name)
    {
        var info = from ca in data.Categories
                   where ca.name_cat == name
                   select new CategoryBLL(ca.id, ca.name_cat);
        return info.First().id;
    }
    public static bool addCategory(string name)
    {
        Category ct = new Category();
        if (ct.name_cat == name)
        {
            return false;
        }
        else
        {
            ct.name_cat = name;
            data.Categories.InsertOnSubmit(ct);
            data.SubmitChanges();
            return true;
        }

    }

    public static bool editCategory(int id, string name)
    {
        try
        {
            Category ct = data.Categories.Single(c => c.id == id);
            ct.name_cat = name;
            data.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool delCategory(int id, string name)
    {
        try
        {
            Category ct = data.Categories.Single(c => c.id == id);
            data.Categories.DeleteOnSubmit(ct);
            data.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }


}
